#include "stdafx.h"
#include "CComingCall.h"
#include "CGraphicMgr.h"
#include "CPhoneStatu.h"
#include "CStaticText.h"


void CALLBACK ONCallTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
   
   
	CStaticText *p=(CStaticText *)GRAPHICMGR.GetElement(L"connecttime");

	
    CComingCallStatu *pStatu=(CComingCallStatu *)GRAPHICMGR.GetStatu(L"comeingcall"); 
	pStatu->Count();
	TCHAR szTimer[256]={0};
	wsprintf(szTimer, L"%02d:%02d", pStatu->GetCount()/60, pStatu->GetCount()%60);
	
	p->setvalue(szTimer);
	
    if (!pStatu->m_bShowButton)
    {
	GRAPHICMGR.SetUpdateElements(L"connecttime",L"none");
	}
	


}
CComingCallStatu::CComingCallStatu(WCHAR * name):IStatu(name)
{
	m_bShowButton=false;
	m_time=0;
	m_szCode[0] = L'\0';
}

CComingCallStatu::~CComingCallStatu(void)
{
}


bool CComingCallStatu::EnterStatu()
{
  

   m_nCountTimer=(CStaticText *)GRAPHICMGR.GetElement(L"connecttime");

   m_pVolSlider=(CSlider*)GRAPHICMGR.GetElement(L"phonein_slider_vol");
   m_pVolSlider->SetRange(0,7);
   m_pVolSlider->SetPos(g_vol);

   m_nCountTimer->setvalue(L"00:00");

   m_nCountTimer->setvisible(true);


   m_pScreenTxt=(CStaticText *)GRAPHICMGR.GetElement(L"comephonenum");

     m_strScreenOut=m_pScreenTxt->getvalue();
   GRAPHICMGR.ShowElements(0,L"callin_btn_0",L"callin_btn_1",\
	   L"callin_btn_2",L"callin_btn_3",\
	   L"callin_btn_4",L"callin_btn_5",\
	   L"callin_btn_6",L"callin_btn_7",\
	   L"callin_btn_8",L"callin_btn_9",\
	   L"callin_btn_x",L"callin_btn_#",L"none");
	return true;

}

bool CComingCallStatu::ExitStatu()

{
	//StopTimer();
	CButton *pBtn=(CButton *)GRAPHICMGR.GetElement(L"phonein_btn_pickup");  
	pBtn->SetEnble(TRUE);
	return FALSE;
}


bool CComingCallStatu::EvaluateClickEvent(const WCHAR* elementName)
{

	
	if (wcscmp(elementName,L"phonein_btn_pickup") == 0)
	   {
	
		      g_pSeries->StopRing();

		    
			 //	RETAILMSG(1,(L"SetPhoneReplyState4\r\n"));
		
			  CDialledNumData* pDialledNum= new  CDialledNumData();
              CStaticText *p= (CStaticText *)GRAPHICMGR.GetElement(L"comephonenum");   

			  pDialledNum->Load(CGenericFun::GetAbsPath(FILE_RECVEDNUMBER));
			  pDialledNum->Insert(0, (WCHAR*)p->getvalue().c_str(), NULL);
			  pDialledNum->Save(CGenericFun::GetAbsPath(FILE_RECVEDNUMBER));
			  delete pDialledNum;
			  pDialledNum = NULL;
			  g_pSeries->SendAnswerCMD();

			  return  TRUE;
	   }
	
	else if (wcscmp(elementName,L"comeingcall_btn_close") == 0)
	{
        
		StopTimer();
	
			//RETAILMSG(1,(L"SetPhoneReplyState5\r\n"));
		 KillTimer(GRAPHICMGR.GetWnd(),PHONE_REPLY);
		 WRITELOG("comeingcall_btn_close");
		g_pSeries->SendHungCallCMD();
		 GRAPHICMGR.BackState();
		return  TRUE;
	}
	else if (wcscmp(elementName,L"phonein_btn_hung") == 0)
		{
			

		
				g_pSeries->StopRing();


			

		
			   g_pSeries->SendHungCallCMD();
				CDialledNumData* pDialledNum= new  CDialledNumData();
				CStaticText *p= (CStaticText *)GRAPHICMGR.GetElement(L"comephonenum");   

				pDialledNum->Load(CGenericFun::GetAbsPath(FILE_MISSEDNUMBER));
				pDialledNum->Insert(0, (WCHAR*)p->getvalue().c_str(), NULL);
				pDialledNum->Save(CGenericFun::GetAbsPath(FILE_MISSEDNUMBER));
				delete pDialledNum;
				pDialledNum = NULL;
				// GRAPHICMGR.BackState();
				 return  TRUE;
	   }
	
	else if(wcscmp(elementName,L"phone_btn_KeyShow") == 0)
	{
          


         ShowKey();
          return true;


	}
	
	else if( wcscmp(elementName,L"phonein_btn_volup") == 0)
	{

		g_vol++;
			if (g_vol>7)
			{
				g_vol=7;
			}
			m_pVolSlider->SetPos(g_vol);
			g_pSeries->SendVolumeUp();
			GRAPHICMGR.SetUpdateElements(L"phonein_slider_vol",L"none");
				return true;
	}
	else if( wcscmp(elementName,L"phonein_btn_voldown") == 0)
	{


		g_vol--;

			if (g_vol<=0)
			{
				g_vol=0;
			}
			m_pVolSlider->SetPos(g_vol);
			g_pSeries->SendVolumeDown();
			GRAPHICMGR.SetUpdateElements(L"phonein_slider_vol",L"none");
				return true;
	}

	else if( wcscmp(elementName,L"callin_btn_0") == 0)

	{ 
		 m_strScreenOut=m_pScreenTxt->getvalue();
	
		wcscpy(m_szCode,m_strScreenOut.c_str());
	    int iStrLength = _tcslen(m_szCode);
			if ( iStrLength >= 128)
			{
				return false;
			}
			m_szCode[iStrLength] = L'0';
			m_szCode[iStrLength +1] = L'\0'; 
			m_pScreenTxt->setvalue(m_szCode);
	    	GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
			
		g_pSeries->SendChar(L'0');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_1") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'1';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");

		g_pSeries->SendChar(L'1');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_2") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'2';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");

		g_pSeries->SendChar(L'2');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_3") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'3';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'3');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_4") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'4';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'4');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_5") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'5';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'5');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_6") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'6';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'6');
		return true;
	}

	else if ( wcscmp(elementName,L"callin_btn_7") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'7';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'7');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_8") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'8';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'8');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_9") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'9';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'9');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_#") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'#';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'#');
		return true;
	}
	else if ( wcscmp(elementName,L"callin_btn_x") == 0)
	{
		m_strScreenOut=m_pScreenTxt->getvalue();

		wcscpy(m_szCode,m_strScreenOut.c_str());
		int iStrLength = _tcslen(m_szCode);
		if ( iStrLength >= 128)
		{
			return false;
		}
		m_szCode[iStrLength] = L'*';
		m_szCode[iStrLength +1] = L'\0'; 
		m_pScreenTxt->setvalue(m_szCode);
		GRAPHICMGR.SetUpdateElements(L"comephonenum",L"none");
		g_pSeries->SendChar(L'*');
		return true;
	}
	return false;
}

void CComingCallStatu::Count()
{
	 m_time++;
}
void CComingCallStatu::StartTimer()
{

	SetTimer(GRAPHICMGR.GetWnd(),CALL_COUNTER,1000,ONCallTimerProc);

   
}
void CComingCallStatu::StopTimer()
{ 
   m_time=0;
   	KillTimer(GRAPHICMGR.GetWnd(),CALL_COUNTER);
}
void CComingCallStatu::ShowKey()
{
	if (m_bShowButton)
	{
		m_bShowButton=false;
	}
	else
		m_bShowButton=true;

	GRAPHICMGR.ShowElements(m_bShowButton,L"callin_btn_0",L"callin_btn_1",\
		L"callin_btn_2",L"callin_btn_3",\
		L"callin_btn_4",L"callin_btn_5",\
		L"callin_btn_6",L"callin_btn_7",\
		L"callin_btn_8",L"callin_btn_9",\
		L"callin_btn_x",L"callin_btn_#",L"none");
	GRAPHICMGR.ShowElements(!m_bShowButton,L"connecttime",L"TalkOutCall",L"Talk_Talking",L"Talk_InCall",L"none");
	CStaticText *p= (CStaticText *)GRAPHICMGR.GetElement(L"connecttime"); 
	p->setvisible(!m_bShowButton);
	if (!m_bShowButton)
	{
		m_pAniCallOut->setvisible(false);
		m_pAniOnCall->setvisible(false);
		m_pAniCallIn->setvisible(false);
		switch(m_nType)
		{
		case  TALK_CALLIN:
			m_pAniCallIn->setvisible(true);
			break;
		case  TALK_CALLOUT:
			m_pAniCallOut->setvisible(true);
			break;
		case  TALK_ONCALL:
			m_pAniOnCall->setvisible(true);
			break;
		}


	}


	GRAPHICMGR.AllElementUpdate();
	/*GRAPHICMGR.SetUpdateElements(L"callin_btn_0",L"callin_btn_1",\
	L"callin_btn_2",L"callin_btn_3",\
	L"callin_btn_4",L"callin_btn_5",\
	L"callin_btn_6",L"callin_btn_7",\
	L"callin_btn_8",L"callin_btn_9",\
	L"callin_btn_x",L"callin_btn_#",L"none");
	*/
	GRAPHICMGR.Refresh();

	
}
void CComingCallStatu::SetStatuType(int nType)
{

	m_nType=nType;
	m_pAniCallOut=(CAniImag*)GRAPHICMGR.GetElement(L"TalkOutCall");
	m_pAniOnCall=(CAniImag*)GRAPHICMGR.GetElement(L"Talk_Talking");
	m_pAniCallIn=(CAniImag*)GRAPHICMGR.GetElement(L"Talk_InCall");
	m_pAniCallOut->setvisible(false);
	m_pAniOnCall->setvisible(false);
	m_pAniCallIn->setvisible(false);
   if (m_bShowButton)
   { 
	   return ;
   }
	switch(m_nType)
	{
	case  TALK_CALLIN:
		m_pAniCallIn->setvisible(true);
		break;
	case  TALK_CALLOUT:
		m_pAniCallOut->setvisible(true);
		break;
	case  TALK_ONCALL:
		m_pAniOnCall->setvisible(true);
		break;


	}
}
